﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.SSO.Core.Entities;
using ESF.General.Entity;
using ESF.SSO.Core.DataAccess;
using ESF.General;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.Data;
using ESF.SSO.Core.DataAccess.BasicReferences;

#endregion


namespace ESF.SSO.Core.BusinessLogic
{
	public static class CtrlEnterprise
	{
		#region Read
		
		public static List<Enterprise> List(string pPartName = null, int? pListCount = null)
		{
			return AdoEnterprise.List (pPartName, pListCount);
		}


		public static Enterprise GetByCode(Enterprise pEnterprise)
		{
			return AdoEnterprise.GetByCode(pEnterprise);
		}
		
		
		public static Enterprise GetByName (Enterprise pEnterprise)
		{
			return AdoEnterprise.GetByName (pEnterprise);
		}
		
		
		public static Enterprise Get (Enterprise pEnterprise)
		{
			#region Validacion
			
			Validate.FilledID ("Identificador", pEnterprise.ID);
			
			#endregion
			
			return AdoEnterprise.Get (pEnterprise);
		}
		
		
		public static BrowseResults<Enterprise> Browse (Enterprise pSample, PaggingInfo pPagInfo)
		{
			return AdoEnterprise.Browse (pSample, pPagInfo);
		}
		
		#endregion
		
		
		#region Write

		public static void Delete(List<Enterprise> pEnterprises)
		{
			using (ContextSingleTransactionScope TS = new ContextSingleTransactionScope (AdoESFSSO.W.ConnectionName))
			{
				foreach (Enterprise e in pEnterprises)
				{
					AdoEnterprise.Delete (e);
				}
				
				TS.Complete ();
			}
		}
		
		
		public static int? Register (Enterprise pEnterprise)
		{
			Terminal tmpTerminal;
			
			
			#region Validacion
			
			Validate.Filled (true, "Nombre", pEnterprise.Name);
			Validate.FilledID ("Tipo de Identificación", pEnterprise.IdentificationType);
			Validate.Filled (true, "Número de Identificación", pEnterprise.IdentificationNumber);
			
			#endregion
			
			
			if (DataHelper.IsEmpty (pEnterprise.Terminals))
			{
				foreach (Terminal ter in pEnterprise.Terminals)
				{
					tmpTerminal = AdoTerminal.Get (ter);

					if (tmpTerminal != null)
					{
						ter.ID = tmpTerminal.ID;
					}
				}
			}

			using (ContextSingleTransactionScope TS = new ContextSingleTransactionScope (AdoESFSSO.W.ConnectionName))
			{
				pEnterprise.ID = AdoEnterprise.Register (pEnterprise);

				TS.Complete ();

				return pEnterprise.ID;
			}
		}
		
		#endregion
	}
}
